// 用户管理模块
const express = require('express');
const router = express.Router();
// 导入数据库
const db = require('../../db/index');
// 用户管理

const { setPassword, getPassword } = require('../../lib/tools');

router.get('/update_pwd', (req, res) => {
  res.render('admin/update_pwd');
});
router.post('/update_pwd', (req, res) => {
  console.log(req.body);
  console.log(req.session.userinfo);
  const username = req.session.userinfo.username
  const oldpassword = req.body.oldpassword
  const newpassword = req.body.newpassword2
  // newpassword = getPassword(newpassword)
  console.log(newpassword);
  const password = getPassword(newpassword)
  console.log(password);
  const sql = `update administrator set password='${password}' where username ='${username}'`;
  db.query(sql, (err, results) => {
    if (err) {
      return res.send({ code: 0, msg: err.message });
    }
    if (results.affectedRows !== 1) {
      // 没有修改成功
      return res.send({ code: 0, msg: '修改失败' });
    } else {
      return res.send({ code: 1, msg: '修改成功' });
    }
  });
});

// 初始化表格
router.get('/query', (req, res) => {
  // 定义一个变量 看一看发来的请求中有search吗
  let isSearch = req.query.search ? req.query.search : '';
  //查询数据总条数
  var count;
  const sqlCount = `select count(*) as count from administrator  where username like "%${isSearch}%"`;
  db.query(sqlCount, (err, result) => {
    if (err) {
      //数据库操作失败
      return err.message;
    }
    // 查询出的数据条数
    count = result[0].count;
  });
  const { page, limit } = req.query;
  var offset = (page - 1) * limit;
  // 前台 发来分页数据
  // console.log(req.query);
  var sql = `select * from administrator where username like "%${isSearch}%" limit ${offset},${limit}`;
  // 查询数据
  // var sql = `select * from administrator`;
  db.query(sql, (err, results) => {
    

    for (let index = 0; index < results.length; index++) {
      results[index].password = setPassword(results[index].password)
    }
    if (err) {
      return res.send({
        code: 1,
        msg: err.message,
      });
    }
    if (results.length) {
      return res.send({
        code: 0,
        msg: '数据查询成功',
        data: results,
        count: count,
      });
    } else {
      return res.send({
        code: 1,
        msg: '没有相应的数据',
      });
    }
  });
});

// 删除
router.get('/delete', (req, res) => {
  const sql = 'delete from administrator where id=?';
  db.query(sql, req.query.id, (err, results) => {
    // console.log(sql);
    if (err) {
      res.send({
        code: 0,
        msg: err.message,
      });
    }
    if (results.affectedRows == 1) {
      return res.send({
        code: 1,
        msg: '删除成功',
      });
    } else {
      return res.send({
        code: 0,
        msg: '删除失败',
      });
    }
  });
  // console.log(req.query.id);
});

// 修改
router.get('/updateAll', (req, res) => {

  // 操作数据库
  const sql = `select * from administrator where id=?`;
  db.query(sql, req.query.id, (err, results) => {
    results[0].password = setPassword(results[0].password)
    if (err) {
      // sql执行失败
      return res.send({
        code: 0,
        msg: err.message,
      });
    }
    if (results.length != 0) {
      // console.log(results);
      return res.send({
        code: 1,
        msg: '查询成功',
        data: results[0],
      });
    } else {
      return res.send({
        code: 0,
        msg: '查询失败',
      });
    }
  });
});
router.post('/updateAll', (req, res) => {
  // 名字不一样  解构不成功！！！！！！！！！！
  const { username, password, id, userpower } = req.body;
  newpassword = getPassword(password)
  // 操作数据库了  update nav set nav_name=nav_name,nav_alias=nav_alias where nav_id =nav_id
  const sql = `update administrator set username='${username}',password='${newpassword}',userpower='${userpower}' where id =${id}`;
  db.query(sql, (err, results) => {
    if (err) {
      return res.send({ code: 0, msg: err.message });
    }
    if (results.affectedRows !== 1) {
      // 没有修改成功
      return res.send({ code: 0, msg: '修改失败' });
    } else {
      return res.send({ code: 1, msg: '修改成功' });
    }
  });
});

// deletecheck
router.get('/deletecheck', (req, res) => {

  var { ids } = req.query;

  var idArr = ids.split(',');

  var sql = `DELETE FROM administrator WHERE id in (${idArr})`;
  db.query(sql, (err, results) => {
    if (err) {
      return res.send({ code: 0, msg: err.message });
    }
  });
  res.send({ code: 1, msg: '删除成功' });
});
module.exports = router;
